import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_history_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';

class MyPropHistoryPage extends StatefulWidget {
  static const path = '/myPropHistoryPage';
  const MyPropHistoryPage({super.key});
  @override
  State<StatefulWidget> createState() => _MyPropHistoryPage();
}

class _MyPropHistoryPage extends State<MyPropHistoryPage> {
  final ScrollController scrollController = ScrollController();
  int pageNum = 1;
  int pageSize = 10;
  final list = RxList<ListByPropIdHistoryItem>([]);
  late String? propId;
  late String propName;

  @override
  void initState() {
    super.initState();
    final params = Get.parameters ?? {};
    final args = Get.arguments ?? {};
    propId = params['propId'] ?? (args['propId'] ?? null);
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        _getPropList(loadMore: true);
      }
    });
    _getPropList();
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  _getPropList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<ListByPropIdHistoryModel>(
        ApiUrl.historyPropList,
        params: {
          'pageNum': pageNum,
          'pageSize': 20,
          'propId': propId == '' ? null : propId,
        },
        success: (result) {
          List<ListByPropIdHistoryItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            list.clear();
          }
          setState(() {
            list.addAll(items);
          });
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;

    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: Stack(
              children: [
                Positioned(
                  right: 0,
                  bottom: 0,
                  child: ImageAssetWidget(
                    imageUrl: 'static/images/bottom_bg.png',
                    width: 258.px,
                    height: 74.px,
                  ),
                ),
                SizedBox(
                  width: context.width,
                  child: ImageAssetWidget(
                    width: 375.px,
                    height: 196.px,
                    alignment: Alignment.topCenter,
                    imageUrl: 'static/images/prop_transfer_bg.png',
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: Column(
                    children: [
                      SizedBox(height: top),
                      CustomAppBar2(title: '历史道具', context: context),
                      Expanded(
                        child: Container(
                          padding: EdgeInsets.fromLTRB(15, 10, 15, 0),
                          child: RefreshAndScrollWidget(
                            onRefresh: _handleRefresh,
                            scrollController: scrollController,
                            itemBuilder: (item, index) => _listItem(item),
                            itemCount: list.length,
                            items: list,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _listItem(item) {
    return Container(
      height: 90.px,
      padding: EdgeInsets.fromLTRB(12.px, 12.px, 21.px, 12.px),
      margin: EdgeInsets.only(bottom: 12.px),
      decoration: BoxDecoration(
        color: AppColors.backgroundColorLightGrey,
        borderRadius: BorderRadius.circular(8.px),
      ),
      child: Row(
        children: [
          LazyImage(
            imageUrl: '${item.propImageUrl}',
            height: 65.px,
            width: 65.px,
            borderRadius: 4.px,
          ),
          hiSpace(width: 16.px),
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '${item.title}',
                style: TextStyle(
                  fontSize: 18.px,
                  color: AppColors.backgroundColorBlack,
                  fontFamily: 'CustomFont',
                ),
              ),
              Text(
                item.useType == 1
                    ? '使用时间${item.useDate}'
                    : '有效时间${item.invalidDate}',
                style: TextStyle(
                  fontSize: 12.px,
                  color: AppColors.fontColorGrey,
                ),
              ),
            ],
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                item.useType == 1 ? '已使用' : '已失效',
                style: TextStyle(
                  fontSize: 14.px,
                  color: AppColors.fontColorBlack,
                  fontFamily: 'CustomFont',
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _handleRefresh() async {
    _getPropList();
  }
}
